package com.wrial.kind.linkedList;
/*
 * @Author  Wrial
 * @Date Created in 18:56 2020/8/7
 * @Description
 */

public class RemoveNthFromEnd {
    public ListNode removeNthFromEnd(ListNode head, int n) {

        ListNode dummy = new ListNode(0);
        dummy.next = head;
        ListNode fastNode = dummy;//快指针（先走n个）
        ListNode slowNode = dummy;//慢指针，慢指针的下一个就是要删除的节点
        for (int i = 0; i < n; i++) {
            fastNode = fastNode.next;
        }
        while (fastNode.next != null) {
            fastNode = fastNode.next;
            slowNode = slowNode.next;
        }
        slowNode.next = slowNode.next.next;
        return dummy.next;

    }

    public class ListNode {
        int val;
        ListNode next;

        ListNode(int x) {
            val = x;
        }
    }
}
